var qrCoderKax01 = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("qrCoderKax01-strings");
    this.logger = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
    this.dLink = '';
  },

  onMenuItemCommand: function(e) {
    var size = 200;
    if (this.dLink.length > 395 ) {
      size = 400;
    }
    qrwindow = window.open(this.getLinkAsQrCode(this.dLink,size),
                            "QR","location=0,status=0,width="+(size+20)+",height="+(size+20));
    //TODO: Move qrWindow to mouse location.
    qrwindow.moveTo(200,200);
  },

  getLinkAsQrCode: function(link,size) {
    //Defaults:
    if(!size) var size=120;
    if(!link){
      var link=this.dLink;
    }else{
      this.dLink = link;
    }

    //Cleaning link:
    var tlink= link.replace(/&/g,'%26');
    tlink=tlink.replace(/ /g,'%20');
    tlink=tlink.replace(/#/g,'%23');

    //Building API call:
    var qrUri = "http:\/\/chart.apis.google.com\/chart?cht=qr&chs="+size+"x"+size+"&chl="+tlink;
    this.logger.logStringMessage('qrCoder requested :'+qrUri);
    return qrUri;
  },

  onToolbarButtonCommand: function(e) {
    qrCoderKax01.onMenuItemCommand(e);
  }
};

window.addEventListener("load", function () { qrCoderKax01.onLoad(); }, false);
